package com.buka.communicate.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.buka.communicate.R;
import com.buka.communicate.activity.CityChooseActivity;
import com.buka.communicate.activity.FamilyCretaeActivity;
import com.buka.communicate.activity.PersonalPageActivity_;
import com.buka.communicate.adapter.ChatRoomOtherAdapter;
import com.buka.communicate.adapter.ChatRoomSuggestAdapter;
import com.buka.communicate.event.EventBuss;
import com.buka.communicate.utils.SpUtil;
import com.buka.communicate.views.banner.Banner;
import com.buka.communicate.views.banner.listener.OnBannerClickListener;
import com.buka.communicate.views.banner.loader.FrescoImageLoader;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import net.neiquan.applibrary.base.BaseFragment;
import net.neiquan.applibrary.base.MyBaseAdapter;
import net.neiquan.applibrary.wight.RefreshLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/11/8 0008.
 */

public class ChatRoomListFragment extends BaseFragment implements RefreshLayout.RetryListener, XRecyclerView.LoadingListener {
    protected int pageNum = 1;
    protected static int PAEG_SIZE = 10;
    protected View view;
    protected MyBaseAdapter adapter;
    RelativeLayout mHeadView;
    @InjectView(R.id.recyclerview)
    XRecyclerView mRecyclerview;
    @InjectView(R.id.refesh_ly)
    RefreshLayout mRefeshLy;
    @InjectView(R.id.mLocationTv)
    TextView mLocationTv;
    @InjectView(R.id.mLocationLL)
    LinearLayout mLocationLL;
    @InjectView(R.id.mFloatActionBtn)
    FloatingActionButton mFloatActionBtn;
    private Banner bannerView;
    private LinearLayout mSuggestLL;
    private RecyclerView mSuggestRecylerView;
    private LinearLayout mOtherLL;
    private ChatRoomSuggestAdapter chatRoomSuggestAdapter;
    private List<String> bannersImg = new ArrayList<String>();

    @Override
    public int getRootViewId() {
        return R.layout.fragment_chat_room_list;
    }

    @Override
    public void setViews() {
        EventBus.getDefault().register(this);
        setAdapter();
        setHeaderViews();
        initBanner();
    }

        /**
     * 普通事件
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN, priority = 100)
    public void onDataSynEvent(EventBuss event) {
        if (event.getState() == EventBuss.CHAT_ROOM_TOP) {
           mRecyclerview.scrollToPosition(0);
        }
    }


    private void setAdapter() {
        adapter = getAdapter();
        mRecyclerview.setAdapter(adapter);
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(getLineNum() == 0 ? 1 : getLineNum(),
                StaggeredGridLayoutManager.VERTICAL);
        layoutManager.setOrientation(StaggeredGridLayoutManager.VERTICAL);
        mRecyclerview.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        mRecyclerview.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
//        mRecyclerview.setArrowImageView(net.neiquan.applibrary.R.mipmap.iconfont_downgrey);
        mRecyclerview.setHasFixedSize(true);
        mRecyclerview.setLayoutManager(layoutManager);
        mRefeshLy.setRetryListener(this);

        mRecyclerview.setLoadingListener(this);
        mRecyclerview.setLoadingMoreEnabled(false);
        mRecyclerview.setPullRefreshEnabled(true);
    }


    private void setHeaderViews() {
        View headView = LayoutInflater.from(getActivity()).inflate(R.layout.layout_chat_room_head, null);
        bannerView = (Banner) headView.findViewById(R.id.mChatRoomBanner);
        mSuggestLL = (LinearLayout) headView.findViewById(R.id.mSuggestLL);
        mSuggestRecylerView = (RecyclerView) headView.findViewById(R.id.mSuggestRecylerView);
        mOtherLL = (LinearLayout) headView.findViewById(R.id.mOtherLL);
        mRecyclerview.addHeaderView(headView);
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2,
                StaggeredGridLayoutManager.VERTICAL);
        layoutManager.setOrientation(StaggeredGridLayoutManager.VERTICAL);
        mSuggestRecylerView.setLayoutManager(layoutManager);
        chatRoomSuggestAdapter = new ChatRoomSuggestAdapter(getActivity(), null);
        mSuggestRecylerView.setAdapter(chatRoomSuggestAdapter);
        chatRoomSuggestAdapter.setOnItemClickListener(new MyBaseAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, List data, int position) {
                startActivity(new Intent(getActivity(), PersonalPageActivity_.class));
            }
        });
    }


    protected MyBaseAdapter getAdapter() {
        return new ChatRoomOtherAdapter(getActivity(), null);
    }

    protected int getLineNum() {
        return 3;
    }

    @Override
    public void initData() {
        adapter.setOnItemClickListener(new MyBaseAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, List data, int position) {
                startActivity(new Intent(getActivity(), PersonalPageActivity_.class));
            }
        });
    }

    @Override
    public void onRetryClick() {

    }

    @Override
    public void onRefresh() {

    }

    @Override
    public void onLoadMore() {

    }

    @OnClick({R.id.mLocationLL, R.id.mFloatActionBtn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.mLocationLL:
                startActivity(new Intent(getActivity(), CityChooseActivity.class));
                break;
            case R.id.mFloatActionBtn:
                startActivity(new Intent(getActivity(), FamilyCretaeActivity.class));
                break;
        }
    }

    private void initBanner() {
        bannersImg.clear();
        bannersImg.add("https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3052930844,3064262324&fm=27&gp=0.jpg");
        bannersImg.add("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2756706171,599458151&fm=27&gp=0.jpg");
        bannersImg.add("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1819487004,4229590880&fm=27&gp=0.jpg");
        bannerView.setImages(bannersImg).setDelayTime(3000)
//                .setBannerAnimation(FlipHorizontalTransformer.class)
                .setImageLoader(new FrescoImageLoader())
                .start();
        bannerView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
        bannerView.setOnBannerClickListener(new OnBannerClickListener() {
            @Override
            public void OnBannerClick(int position) {

            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.inject(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
        EventBus.getDefault().unregister(this);
    }


}
